package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.JsonException;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.dto.LabelValue;
import com.ugoodtech.mdcc.core.domain.User;
import org.hibernate.HibernateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright © 2013 All Rights Reserved, Ugood Technology, LLC.
 * <p/>
 * User: Stone
 */
public class BaseController {
//    @Value("${logger.skip}")
//    public Boolean loggerSkip;

    public static final Logger log = LoggerFactory.getLogger(BaseController.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResponse resolveException(HttpServletRequest req, HttpServletResponse resp, Exception exp) {
        JsonResponse jsonResponse = new JsonResponse();
        if (exp instanceof JsonException) {
            jsonResponse.setSuccess(true);
            jsonResponse.setErrorDescription(exp.getMessage());
            if (((JsonException) exp).getErrCode() != null) {
                jsonResponse.setError(((JsonException) exp).getErrCode());
            }
            resp.setStatus(HttpServletResponse.SC_OK);
//            exp.printStackTrace();
        } else if (exp instanceof HibernateException) {
            jsonResponse.setSuccess(false);
            jsonResponse.setErrorDescription(exp.getMessage());
            resp.setStatus(HttpServletResponse.SC_OK);
            exp.printStackTrace();
        } else {
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            jsonResponse.setSuccess(false);
            jsonResponse.setErrorDescription(exp.getMessage());
            exp.printStackTrace();
        }
        return jsonResponse;
    }

    public List<String[]> getLabelValueArrayForString(List<LabelValue> labelValues, boolean addAll) {
        List<String[]> dataList = new ArrayList<String[]>();
        for (LabelValue labelValue : labelValues) {
            dataList.add(new String[]{labelValue.getValue(), labelValue.getLabel()});
        }
        if (addAll) {
            dataList.add(new String[]{null, "全部"});
        }
        return dataList;
    }

    protected User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal != null && principal instanceof User) {
            return (User) principal;
        } else {
            Object details = authentication.getDetails();
            if (details != null && details instanceof User) {
                return (User) details;
            }
        }
        return null;
    }

}
